<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar" :src="userObj.photo" @click="btn" />
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="userObj.name" @click="nameFn" />
      <van-cell title="性别" is-link :value="sexobj[userObj.gender]" />
      <van-cell title="生日" is-link :value="userObj.birthday" @click="birthFn"/>
    </van-cell-group>
    <!-- 修改用户名 -->
    <van-dialog
      v-model="show"
      title="标题"
      show-cancel-button
      :before-close="besorecloseFn"
    >
      <input type="text" v-model="eaditName" ref="inp" />
    </van-dialog>
    <!-- 弹出框时间 -->
    <van-popup v-model="popshow" position="bottom" :style="{width:'100%', height: '45%' }" round>
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="confirmFn"
      />
    </van-popup>
  </div>
</template>

<script>
import { userProfileAPI, updataPhotoAPI, updataProfileAPI } from '@/api'
import { Notify } from 'vant'
import moment from 'moment'
export default {
  name: 'UserEdit',
  data () {
    return {
      userObj: {},
      sexobj: {
        1: '男',
        0: '女'
      },
      show: false,
      eaditName: '',
      popshow: false,
      minDate: new Date(1920, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(2021, 0, 17)
    }
  },
  async created () {
    const [, res] = await userProfileAPI()
    console.log(res)
    this.userObj = res.data.data
  },
  methods: {
    async onFileChange (e) {
      const fd = new FormData()
      //   console.log(e.target.files)
      fd.append('photo', e.target.files[0])

      const [err, res] = await updataPhotoAPI(fd)
      if (!err) this.userObj.photo = res.data.data.photo
    },
    btn () {
      this.$refs.iptFile.click()
    },
    nameFn () {
      this.show = true
      this.eaditName = this.userObj.name
      this.$nextTick(() => {
        this.$refs.inp.focus()
      })
    },
    async besorecloseFn (action, done) {
      if (action === 'cancel') {
        done()
      } else {
        // 点确定
        // 校验输入框值是否合法
        if (/^[\u4e00-\u9fa5|a-zA-Z0-9]{2,8}$/.test(this.eaditName)) {
          // 通过
          // 发请求
          const [err] = await updataProfileAPI({
            name: this.eaditName
          })
          if (!err) {
            done()
            Notify({ type: 'success', message: '修改成功' })
            this.userObj.name = this.eaditName
          } else {
            Notify({ type: 'danger', message: '用户重名了' })
            done(false) // 弹窗不能关闭
          }
        } else {
          // 没通过
          Notify({ type: 'danger', message: '用户名只能2-8位中文字母和数字' })
          done(false)
        }
      }
    },
    birthFn () {
      this.popshow = true
      this.currentDate = new Date(this.userObj.birthday)
    },
    async confirmFn (value) {
      const data = moment(value).format('YYYY - MM - DD')
      const [err] = await updataProfileAPI({
        birthday: data
      })
      if (!err) {
        this.popshow = false
        this.userObj.birthday = data
      }
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
/deep/ .van-dialog__content {
  text-align: center;
  input {
    border: none;
    text-align: center;
  }
}
</style>
